package cn.sibu.plugin.authorize.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.sibu.plugin.authorize.R;
import cn.sibu.plugin.authorize.bean.LoginEvent;
import cn.sibu.plugin.authorize.http.SibuAuthorizeHttp;
import cn.sibu.plugin.authorize.util.ActivityLifeCycle;
import cn.sibu.plugin.authorize.util.CommonUtil;
import cn.sibu.plugin.authorize.util.DialogUtil;
import cn.sibu.plugin.authorize.util.ErrorContent;
import cn.sibu.plugin.authorize.util.L;
import cn.sibu.plugin.authorize.util.RegexRule;
import cn.sibu.plugin.authorize.util.RequestParamName;
import cn.sibu.plugin.authorize.util.ResponseCode;
import cn.sibu.plugin.authorize.view.EditTextWithPwd;
import core.dl.internal.DLIntent;
import de.greenrobot.event.EventBus;

/**
 * 
 * TODO： 用来登录到应用的Activity
 * 
 * @data: 2014-11-17 上午11:03:09
 * @version: V1.0
 */
public class LoginActivity extends BaseActivity implements OnClickListener {

	@InjectView(R.id.et_login_num)
	EditText et_loginNum;
	@InjectView(R.id.et_login_password)
	EditTextWithPwd et_loginPwd;

	@InjectView(R.id.btn_login)
	Button btn_login;
	@InjectView(R.id.btn_forgetpwd)
	TextView btn_forgetpwd;
	@InjectView(R.id.tv_sibu)
	TextView tv_sibu;
	private String loginNum;
	private String loginPwd;

	
	protected static final int MSG_LOGIN = 1; 
	protected static final int MSG_LOGINERR =2;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login_home);
		ButterKnife.inject(this);
		EventBus.getDefault().register(this);
		initView();
		/**
		 * token过期无效，弹出提示
		 */
		String tokenstatus = getIntent().getStringExtra(ResponseCode.TOKENVOID);
		if (!TextUtils.isEmpty(tokenstatus)
				&& tokenstatus.equals(ResponseCode.TOKENVOID)) {
			DialogUtil.ShowReLogin(that);
		}
	}
//	public void onDestroy(){
//		EventBus.getDefault().unregister(this);
//		super.onDestroy();
//	}
	/**
	 * TODO 初始化控件的方法
	 * 
	 * @param
	 * @throw
	 * @return void
	 */
	public void initView() {

		btn_login.setOnClickListener(this);
		btn_forgetpwd.setOnClickListener(this);
		tv_sibu.setOnClickListener(this);
	}
	/**
	 * 按钮的点击事件
	 */
	//private boolean ispress = true;
	@Override
	public void onClick(View v) {
		// 防止暴力点击
		/* if (ButtonAvoidForceUtils.filter()) {*/
			
			switch (v.getId()) {
				case R.id.btn_login:
					  if(CommonUtil.isNetworkAvailable(that)==0){
							Toast.makeText(that,"当前手机没有联网，请进行联网操作！", Toast.LENGTH_LONG).show();
							 return;
						}
				   /*// 隐藏软键盘
						InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
						imm.hideSoftInputFromWindow(
								btn_login.getWindowToken(), 0);*/
						login();
					 break;
				case R.id.tv_sibu:// 跳转注册界面
	//				Intent registerintent = new Intent(this,
	//						InviteCodeActivity.class);
	//				startActivity(registerintent);
					break;
				case R.id.btn_forgetpwd:// 跳转到忘记密码页面
					L.e("点击忘记密码");
					DLIntent intent = new DLIntent(getPackageName(),
							ForgetPasswordActivity.class);
					ActivityLifeCycle.removeAllActivity();
					startPluginActivity(intent);
					finish();
					break;
			}
 	}
	 

	/**
	 * 
	 * TODO 登录操作的方法
	 * 
	 * @param
	 * @throw
	 * @return void
	 */
	public void login() {
		//设置登录按钮不可点击
		btn_login.setClickable(false);
		loginNum = et_loginNum.getText().toString().trim();
		// loginPwd = et_loginPwd.getText().toString().trim();
		loginPwd =  et_loginPwd.getPwd();
		// 需要进行相应的逻辑判断： 1. 判断用户名密码是否正确
		if (TextUtils.isEmpty(loginNum)) {
			btn_login.setClickable(true);
			Toast.makeText(that, ErrorContent.LOGININFONULL,
					Toast.LENGTH_SHORT).show();
			return;
		}
		if (TextUtils.isEmpty(loginPwd)) {
			btn_login.setClickable(true);
			Toast.makeText(that, ErrorContent.PASSWORDNULL,
					Toast.LENGTH_SHORT).show();
			return;
		}
		// 用户名、密码匹配正则
		if (CommonUtil.regexMate(RegexRule.PHONENUM, loginNum)
				|| CommonUtil.regexMate(RegexRule.USERNAME, loginNum)) {

			if (!CommonUtil.regexMate(RegexRule.PASSWORD, loginPwd)) {
				btn_login.setClickable(true);
				Toast.makeText(that,
						ErrorContent.PASSWORDWRONG, Toast.LENGTH_SHORT)
						.show();
				return;
			}

			//缓冲效果
			DialogUtil.showDialogForLoading(that, "正在加载...", true);
			//请求网络
			SibuAuthorizeHttp.login(loginNum, loginPwd, that);
		} else {
			btn_login.setClickable(true);
			Toast.makeText(that,
					ErrorContent.PHONEUSERNAMEILLEAGAL, Toast.LENGTH_SHORT)
					.show();
			return;
		}

	}

	/**
	 * 接受登录请求的回调
	 * @param event
	 */
	public void onEventMainThread(LoginEvent event) {
		btn_login.setClickable(true);
		DialogUtil.dismissDialogForLoading();
		if(event.code==null){
			Toast.makeText(that, "连接失败", Toast.LENGTH_LONG).show();
			return;
		}
		if (ResponseCode.SUCCEED.equals(event.code)) { //登录通过
			L.e("登录通过");
//			 跳入登录验证页面
			DLIntent intent = new DLIntent(getPackageName(),
					AuthcodeActivity.class);
			intent.putExtra("mobile", event.mobile);
			intent.putExtra("loginNum", loginNum);
			intent.putExtra("loginPwd", loginPwd);
			intent.putExtra(AuthcodeActivity.CODETYPE, RequestParamName.LOGIN_AUTHCODE);//登录请求验证码意图
			startPluginActivity(intent);
		}else if (ResponseCode.UNFINISHEDUSERINFO.equals(event.code)) {//未完善基本用户信息

		} else if (ResponseCode.UNBINDPHONE.equals(event.code)) {// 如果返回的绑定手机号字符串为空，则跳转到绑定手机页面
//			SharedPreferencesUitls.saveString(that,
//					ConfigKeyName.USERNAME, loginNum);
//



//			Intent phonebindIntent = new Intent(that,
//					PhoneBindActivity.class);
//
//			startActivity(phonebindIntent);
//			finish();

		}else{
			Toast.makeText(that, event.msg, Toast.LENGTH_LONG).show();
			return;
		}
	}

	// 处理返回键退出程序,按俩次返回键推出程序
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
//		if (keyCode == KeyEvent.KEYCODE_BACK
//				&& event.getAction() == KeyEvent.ACTION_DOWN) {
//			ExitApplication.getInstance().exit();
//		}
		return super.onKeyDown(keyCode, event);
	}
}
